<template>
  <div class="mobile-order">
    <!-- 顶部导航栏 -->
    <div class="mobile-nav-bar">
      <div class="nav-left" @click="goBack">
        <el-icon><ArrowLeft /></el-icon>
      </div>
      <div class="nav-title">我的订单</div>
      <div class="nav-right"></div>
    </div>

    <!-- 订单列表 -->
    <div class="order-list">
      <transition-group name="fade-list" tag="div" class="order-items">
        <template v-if="orderList.length === 0">
          <div class="empty-state" key="empty">
            <el-empty description="暂无订单" :image-size="150">
              <template #description>
                <p class="empty-text">您还没有订单记录</p>
              </template>
            </el-empty>
          </div>
        </template>
        
        <div v-else v-for="order in orderList" :key="order.oid" class="order-item">
          <div class="order-header">
            <div class="order-number">
              <span class="label">订单号:</span>
              <span class="value">{{ order.oid }}</span>
            </div>
            <div class="order-status" :class="getStatusClass(order.status)">
              {{ getStatusText(order.status) }}
            </div>
          </div>
          
          <div class="order-content">
            <div class="movie-poster">
              <!-- 这里可以添加电影海报，如果API支持的话 -->
              <div class="poster-placeholder">
                <el-icon><VideoCamera /></el-icon>
              </div>
            </div>
            <div class="movie-info">
              <div class="movie-name">{{ order.mname }}</div>
              <div class="movie-meta">
                <el-icon><User /></el-icon>
                <span>导演: {{ order.director }}</span>
              </div>
              <div class="movie-meta">
                <el-icon><Calendar /></el-icon>
                <span>场次: {{ order.sname }}</span>
              </div>
              <div class="movie-meta">
                <el-icon><Location /></el-icon>
                <span>座位: {{ getSeat(order.message) }}</span>
              </div>
            </div>
          </div>
          
          <div class="order-footer">
            <div class="order-price-time">
              <div class="order-price">¥{{ order.amount }}</div>
              <div class="order-time">
                <el-icon><Clock /></el-icon>
                <span>{{ formatTime(order.buyTime) }}</span>
              </div>
            </div>
            
            <div class="order-actions">
              <el-button 
                v-if="order.status === '待支付'" 
                type="danger" 
                size="small" 
                class="action-button"
                @click="cancelOrder(order.oid)"
              >
                取消订单
              </el-button>
            </div>
          </div>
        </div>
      </transition-group>

      <!-- 分页 -->
      <div class="mobile-pagination" v-if="total > pageSize">
        <el-pagination
          v-model:current-page="currentPage"
          :page-size="pageSize"
          layout="prev, pager, next"
          :total="total"
          @current-change="handlePageChange"
          background
          hide-on-single-page
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage, ElMessageBox } from 'element-plus';
import { ArrowLeft, VideoCamera, User, Calendar, Location, Clock } from '@element-plus/icons-vue';
import { mobileGetUserOrder } from '@/api/mobile';
import { getSeat, formatTime } from "@/util/common";
import qs from 'qs';

const router = useRouter();
const orderList = ref([]);
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);
const loading = ref(false);
const userId = ref(localStorage.getItem("id"));

// 返回上一页
const goBack = () => {
  router.back();
};

// 获取订单列表
const getOrderList = async () => {
  loading.value = true;
  try {
    const result = await mobileGetUserOrder(qs.stringify({
      userId: userId.value,
      page: currentPage.value,
      pageSize: pageSize.value
    }));
    
    if (result.code === 1) {
      orderList.value = result.data.records || [];
      total.value = result.data.total || 0;
    } else {
      ElMessage.error(result.message || '获取订单列表失败');
    }
  } catch (error) {
    console.error('获取订单列表出错', error);
    ElMessage.error('获取订单列表失败');
  } finally {
    loading.value = false;
  }
};

// 处理分页变化
const handlePageChange = (page) => {
  currentPage.value = page;
  getOrderList();
};

// 取消订单
const cancelOrder = (orderId) => {
  ElMessageBox.confirm(
    '确定要取消该订单吗？',
    '取消订单',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    // 这里应该调用取消订单的API
    ElMessage.success('订单取消成功');
    // 刷新订单列表
    getOrderList();
  }).catch(() => {});
};

// 获取订单状态文本
const getStatusText = (status) => {
  switch (status) {
    case "待支付":
      return '待支付';
    case '已支付':
      return '已支付';
    case '已取消':
      return '已取消';
    default:
      return '未知状态';
  }
};

// 获取订单状态样式类
const getStatusClass = (status) => {
  switch (status) {
    case "待支付":
      return 'status-pending';
    case '已支付':
      return 'status-paid';
    case '已取消':
      return 'status-canceled';
    default:
      return '';
  }
};

onMounted(() => {
  getOrderList();
});
</script>

<style scoped lang="scss">
.mobile-order {
  min-height: 100vh;
  background-color: #f8f9fa;
  padding-bottom: 60px;
  font-family: 'PingFang SC', 'Helvetica Neue', Helvetica, Arial, sans-serif;
}

.mobile-nav-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  background: linear-gradient(135deg, #ff6b6b, #ff8e8e);
  color: white;
  box-shadow: 0 4px 12px rgba(255, 107, 107, 0.2);
  position: sticky;
  top: 0;
  z-index: 10;
  border-radius: 0 0 16px 16px;
  
  .nav-left {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    background-color: rgba(255, 255, 255, 0.2);
    cursor: pointer;
    transition: all 0.3s ease;
    
    &:hover {
      background-color: rgba(255, 255, 255, 0.3);
    }
    
    .el-icon {
      font-size: 18px;
      color: white;
    }
  }
  
  .nav-title {
    font-size: 18px;
    font-weight: 600;
    letter-spacing: 0.5px;
  }
  
  .nav-right {
    width: 32px;
  }
}

.order-list {
  padding: 16px;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 0;
  
  .empty-text {
    color: #909399;
    font-size: 16px;
    margin-top: 8px;
  }
}

.order-items {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.order-item {
  background-color: white;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.05);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  
  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.08);
  }
}

.order-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px;
  background-color: #f9f9f9;
  border-bottom: 1px solid #f0f0f0;
  
  .order-number {
    font-size: 14px;
    color: #606266;
    
    .label {
      color: #909399;
      margin-right: 4px;
    }
    
    .value {
      font-family: 'Roboto Mono', monospace;
      letter-spacing: 0.5px;
    }
  }
  
  .order-status {
    font-size: 14px;
    font-weight: 600;
    padding: 4px 10px;
    border-radius: 12px;
    
    &.status-pending {
      background-color: rgba(230, 162, 60, 0.1);
      color: #e6a23c;
    }
    
    &.status-paid {
      background-color: rgba(103, 194, 58, 0.1);
      color: #67c23a;
    }
    
    &.status-canceled {
      background-color: rgba(144, 147, 153, 0.1);
      color: #909399;
    }
  }
}

.order-content {
  display: flex;
  padding: 16px;
  
  .movie-poster {
    width: 80px;
    height: 110px;
    border-radius: 8px;
    overflow: hidden;
    margin-right: 16px;
    background-color: #f0f0f0;
    
    .poster-placeholder {
      width: 100%;
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: center;
      background: linear-gradient(135deg, #f0f0f0, #e0e0e0);
      
      .el-icon {
        font-size: 32px;
        color: #bdbdbd;
      }
    }
  }
  
  .movie-info {
    flex: 1;
    
    .movie-name {
      font-size: 18px;
      font-weight: 600;
      margin-bottom: 10px;
      color: #303133;
      line-height: 1.3;
    }
    
    .movie-meta {
      display: flex;
      align-items: center;
      font-size: 14px;
      color: #606266;
      margin-bottom: 8px;
      
      .el-icon {
        font-size: 16px;
        margin-right: 6px;
        color: #909399;
      }
    }
  }
}

.order-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px;
  border-top: 1px solid #f0f0f0;
  
  .order-price-time {
    display: flex;
    flex-direction: column;
    
    .order-price {
      font-size: 20px;
      font-weight: 600;
      color: #ff6b6b;
      margin-bottom: 6px;
    }
    
    .order-time {
      display: flex;
      align-items: center;
      font-size: 12px;
      color: #909399;
      
      .el-icon {
        font-size: 14px;
        margin-right: 4px;
      }
    }
  }
  
  .order-actions {
    .action-button {
      border-radius: 20px;
      font-weight: 500;
      transition: all 0.3s ease;
      
      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 8px rgba(255, 107, 107, 0.2);
      }
    }
  }
}

.mobile-pagination {
  margin-top: 24px;
  display: flex;
  justify-content: center;
}

// 动画效果
.fade-list-enter-active,
.fade-list-leave-active {
  transition: all 0.5s ease;
}

.fade-list-enter-from {
  opacity: 0;
  transform: translateY(20px);
}

.fade-list-leave-to {
  opacity: 0;
  transform: translateY(-20px);
}
</style>